package com.cfun.mylearn.Support.module.Dagger2;

import com.cfun.mylearn.Support.api.ZheDailyApiService;

import java.util.LinkedList;
import java.util.List;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by flyop on 19/06/2016.
 */

@Module
@Singleton
public class RetrofitModule
{
    private static final String BASE_URL = "";

    @Provides
    OkHttpClient provideHttpClient()
    {
        OkHttpClient client = new OkHttpClient();
        return client;
    }

    @Provides
    List<CallAdapter.Factory> provideCallAdapterFactories(){
        List<CallAdapter.Factory> factoryList = new LinkedList<>();
        factoryList.add(RxJavaCallAdapterFactory.create());
        return factoryList;
    }

    @Provides
    List<Converter.Factory> provideConverterFactories(){
        List<Converter.Factory> factoryList = new LinkedList<>();
        factoryList.add(GsonConverterFactory.create());
        return factoryList;
    }

    @Provides
    Retrofit provideRetrofit(OkHttpClient httpClient, List<CallAdapter.Factory> callAdapaters,List<Converter.Factory> converters ){
        Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(httpClient);

        for (CallAdapter.Factory fac: callAdapaters)
        {
            retrofitBuilder.addCallAdapterFactory(fac);
        }

        for (Converter.Factory fac: converters)
        {
            retrofitBuilder.addConverterFactory(fac);
        }
        return retrofitBuilder.build();
    }

    @Provides
    ZheDailyApiService provideApiService(Retrofit retrofit)
    {
        return  retrofit.create(ZheDailyApiService.class);
    }
}
